﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsForms
{
    public partial class FrmActivityManage : Form
    {
        private int num;

        public FrmActivityManage()
        {
            InitializeComponent();
            #region   初始化控件缩放

            x = Width;
            y = Height;
            setTag(this);

            #endregion
        }

        private void dgvActivity_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Activity cm = (Activity)dgvActivity.CurrentRow.DataBoundItem;
            LoadDetail(cm);
        }


        

        private void FrmActivityManage2_Load(object sender, EventArgs e)
        {
            ActivityManage activitybll = new ActivityManage();
            dgvActivity.AutoGenerateColumns = false;
            string sql = "SELECT * FROM activity,club where activity.clubid=club.clubid";
            dgvActivity.DataSource = activitybll.GetALLActivities(sql);



            //添加ListBox列表项
            Activity act1 = new Activity(1, 2, "太湖西山岛", Convert.ToDateTime("2018-12-2"), "野外摄影", 100, 30);
            Activity act2 = new Activity(2, 1, "大学生活动中心前广场", Convert.ToDateTime("2019-12-20"), "冬季爱心捐款", 120, 20);
            Activity act3 = new Activity(3, 1, "社区养老院", Convert.ToDateTime("2019-5-1"), "看望社区孤老", 200, 20);
            Activity act4 = new Activity(4, 4, "A楼302教室", Convert.ToDateTime("2019-6-6"), "写作欣赏", 230, 10);
            lstActivityList.Items.Add(act1);
            lstActivityList.Items.Add(act2);
            lstActivityList.Items.Add(act3);
            lstActivityList.Items.Add(act4);
            lstActivityList.DisplayMember = "Activityname";



            Activity cm = (Activity)dgvActivity.CurrentRow.DataBoundItem;
            LoadDetail(cm);
        }

        

        private void LoadDetail(Activity cm)
        {
                txtName.Text = cm.Activityname;
                dtpDate.Value = cm.Activitydate;
                txtPlace.Text = cm.Place;
                txtExpenditure.Text = Convert.ToString(cm.Expenditure);
        }


        private void lstActivityList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstActivityList.SelectedIndex != -1)
            {
                Activity cm = (Activity)lstActivityList.SelectedItem;
                txtName.Text = cm.Activityname;
                dtpDate.Value = cm.Activitydate;
                txtPlace.Text = cm.Place;
                txtExpenditure.Text = Convert.ToString(cm.Expenditure);
            }
        }
        //删除
        private void btnDel_Click(object sender, EventArgs e)
        {
            if (lstActivityList.SelectedIndex != -1)
            {
                lstActivityList.Items.Remove(lstActivityList.SelectedItem);
                lstActivityList.SelectedIndex = 0;
            }

            int activityid = 0;
            int clubid = 0;
            string place = txtPlace.Text;
            string activityname = txtName.Text;
            DateTime activitydate = dtpDate.Value;
            float expenditure = float.Parse(txtExpenditure.Text);

            Activity cm = new Activity(activityid, clubid, place, activitydate, activityname, expenditure, num);
            ActivityManage activitybll = new ActivityManage();
            if (activitybll.DeleteActivity(cm))
                MessageBox.Show("社员删除成功!");

        }
        //添加
        private void btnAdd_Click(object sender, EventArgs e)
        {
            ActivityManage memberbll = new ActivityManage();
            string sql = "SELECT * FROM activity,club where activity.clubid=club.clubid";
            memberbll.GetALLActivities(sql);
            int activityid =int.Parse( memberbll.GetALLActivities(sql).Count.ToString())+1;
            int clubid = int.Parse(memberbll.GetALLActivities(sql).Count.ToString())+1;
            string place = txtPlace.Text;
            string activityname = txtName.Text;
            DateTime activitydate = dtpDate.Value;
            float expenditure = float.Parse(txtExpenditure.Text);

            Activity cm = new Activity(activityid, clubid, place, activitydate, activityname, expenditure, num);
            lstActivityList.Items.Add(cm);


            ActivityManage activitybll = new ActivityManage();
            if (activitybll.AddActivity(cm))
                MessageBox.Show("社员添加成功!");
        }
        //修改
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            //这个操作是为了防止数据clubid和activityid为0查询不到这种情况
            ActivityManage memberbll = new ActivityManage();
            string sql = "SELECT * FROM activity,club where activity.clubid=club.clubid";
            memberbll.GetALLActivities(sql);
            int activityid = int.Parse(memberbll.GetALLActivities(sql).Count.ToString());
            int clubid = int.Parse(memberbll.GetALLActivities(sql).Count.ToString());


            string place = txtPlace.Text;
            string activityname = txtName.Text;
            DateTime activitydate = dtpDate.Value;
            float expenditure = float.Parse(txtExpenditure.Text);

            Activity cm = new Activity(activityid, clubid, place, activitydate, activityname, expenditure, num);
            
            ActivityManage activitybll = new ActivityManage();
            if (activitybll.UpdateActivity(cm))
                MessageBox.Show("社员修改成功!");
        }
        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            ActivityManage memberbll = new ActivityManage();
            string sql = "SELECT * FROM activity,club where activity.clubid=club.clubid";
            memberbll.GetALLActivities(sql);
            int activityid = int.Parse(memberbll.GetALLActivities(sql).Count.ToString()) + 1;
            int clubid = int.Parse(memberbll.GetALLActivities(sql).Count.ToString()) + 1;
            string place = txtPlace.Text;
            string activityname = txtName.Text;
            DateTime activitydate = dtpDate.Value;
            float expenditure = float.Parse(txtExpenditure.Text);

            Activity cm = new Activity(activityid, clubid, place, activitydate, activityname, expenditure, num);
            
            ActivityManage activitybll = new ActivityManage();
            if (activitybll.AddActivity(cm))
                MessageBox.Show("社员保存成功!");
        }

        private void FrmActivityManage_Resize(object sender, EventArgs e)
        {
            
                //重置窗口布局
                ReWinformLayout();
            
        }

        #region 控件大小随窗体大小等比例缩放

        private readonly float x; //定义当前窗体的宽度
        private readonly float y; //定义当前窗体的高度

        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0) setTag(con);
            }
        }

        private void setControls(float newx, float newy, Control cons)
        {
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
                //获取控件的Tag属性值，并分割后存储字符串数组
                if (con.Tag != null)
                {
                    var mytag = con.Tag.ToString().Split(';');
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * newx); //宽度
                    con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * newy); //高度
                    con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * newx); //左边距
                    con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * newy); //顶边距
                    var currentSize = Convert.ToSingle(mytag[4]) * newy; //字体大小                   
                    if (currentSize > 0) con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    con.Focus();
                    if (con.Controls.Count > 0) setControls(newx, newy, con);
                }
        }


        /// <summary>
        /// 重置窗体布局
        /// </summary>
        private void ReWinformLayout()
        {
            var newx = Width / x;
            var newy = Height / y;
            setControls(newx, newy, this);

        }

        #endregion
    }
}
